<div class="portlet box grey">
    <div class="portlet-title">
        <div class="caption">
            <a class="btn green float-left" href="<?php echo url_for('@product_add') ?>" ><span class="icon-plus"></span> Бараа Нэмэх</a>
        </div>
        <div class="tools">
            <a href="javascript:;" class="collapse"></a>
        </div>
        <form method="get" class="form-horizontal">
            <div class="control-group float-left">
                <select name="categoryId">
                    <option value="0">Ангилал </option>
                    <?php foreach ($categories AS $category): ?>
                        <option value="<?php echo $category['id'] ?>" <?php echo $category['id'] == $categoryId ? 'selected' : '' ?>><?php echo $category['name'] ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="control-group float-left">
                <select name="manufacturerId">
                    <option value="0">Үйлдвэрлэгч </option>
                    <?php foreach ($manufacturers AS $manufacturer): ?>
                        <option value="<?php echo $manufacturer['id'] ?>" <?php echo $manufacturer['id'] == $manufacturerId ? 'selected' : '' ?>><?php echo $manufacturer['name'] ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="control-group float-left">
                <select name="isActive">
                    <option value="0">Төлөв </option>
                    <option value="<?php echo ProductTable::ACTIVE ?>" <?php echo $isActive == ProductTable::ACTIVE ? 'selected' : '' ?>>Идэвхтэй</option>
                    <option value="<?php echo ProductTable::PASSIVE ?>" <?php echo $isActive != null && $isActive == ProductTable::PASSIVE ? 'selected' : '' ?>>Устсан</option>
                </select>       
            </div>
            <div class="control-group float-left">
                <input type="text" name="price" value="<?php echo $price ? $price : '' ?>" style="width: 100px" placeholder="Үнэ" />
            </div>
            <div class="control-group float-left">
                <input type="text" name="price_sale" value="<?php echo $priceSale ? $priceSale : '' ?>" style="width: 100px" placeholder="Үнэ/Sale/" />
            </div>
            <div class="control-group float-left">
                <button type="submit" class="btn btn-primary">
                    <i class="icon-filter icon-white"></i> Шүүх
                </button>
            </div>
        </form>
    </div>
    <div class="portlet-body">
        <div id="productTableContainer" style="clear:both"></div>
    </div>
</div>

<script type="text/javascript">

    $(document).ready(function() {
        $('#productTableContainer').jtable({
            paging: true,
            pageSize: 30,
            sorting: true,
            defaultSorting: 'Name ASC',
            ajaxSettings: {
                type: 'POST',
                dataType: 'json'
            },
            actions: {
                listAction: '<?php echo $tableUrl ?>'
            },
            fields: {
                id: {
                    key: true,
                    create: false,
                    edit: false,
                    list: false
                },
                img: {
                    title: 'Зураг',
                    sorting: false,
                    width: '10%',
                    display: function(data) {
                        var img = 'none';
                        if (data.record.image_name) {
                            img = data.record.image_name;
                        }
                        if (data.record.main_image_name) {
                            img = data.record.main_image_name;
                        }
                        if (img === 'none') {
                            return 'Зураггүй';
                        }
                        return '<img src="<?php echo AppTools::getFullUrl() . '/uploads' . AppTools::PRODUCT_IMAGE_PATH_THUMB ?>' + img + '" />';
                    }
                },
                name: {
                    title: 'Нэр',
                    width: '12%'
                },
                cat_name: {
                    title: 'Ангилал',
                    width: '8%'
                },
                man_name: {
                    title: 'Үйлдвэрлэгч',
                    width: '8%'
                },
                type: {
                    title: 'Төрөл',
                    width: '6%',
                    options: <?php echo json_encode(ProductTable::getTypeForSelect()) ?>
                },
                intro_description: {
                    title: 'Товч тайлбар',
                    width: '14%',
                    sorting: false
                },
                price: {
                    title: 'Үнэ',
                    width: '8%'
                },
                price_sale: {
                    title: 'Үнэ/Sale/',
                    width: '8%'
                },
                sale_date: {
                    title: 'Sale хугацаа',
                    width: '8%'
                },
                code: {
                    title: 'Код',
                    width: '8%'
                },
                is_active: {
                    title: 'Төлөв',
                    width: '4%',
                    options: {'1': 'Идэвхтэй', '2': 'Идэвхгүй'}
                },
                created_at: {
                    title: 'Бүртгэсэн огноо',
                    width: '10%'
                },
                actions: {
                    sorting: false,
                    width: '12%',
                    display: function(data) {
                        ret = '<a class="btn mini green" href="<?php echo $updateUrl ?>/' + data.record.id + '"><i class="icon-pencil"> Засах</i></a>&nbsp;';
                        if (data.record.is_active == 1) {
                            ret += '<a class="btn mini red" onclick="return confirm(\'Та устгахдаа итгэлтэй байна уу?\');" href="<?php echo $deleteUrl ?>/' + data.record.id + '"><i class="icon-trash"> Устгах</i></a>';
                        } else {
                            ret += '<a class="btn mini blue" onclick="return confirm(\'Та сэргээхдээ итгэлтэй байна уу?\');" href="<?php echo $restoreUrl ?>/' + data.record.id + '"><i class="icon-refresh"> Сэргээх</i></a>';
                        }
                        return ret;
                    }
                }
            }
        });
        $('#productTableContainer').jtable('load');
    });
</script>